package com.devsoft.foodfrenzy;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.AutoWrap;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.util.GLState;
import org.andengine.util.adt.align.HorizontalAlign;
import org.andengine.util.adt.color.Color;
import com.devsoft.foodfrenzy.SceneManager.SceneType;

public class AchievementsScene extends BaseScene implements IOnMenuItemClickListener{
	
	private HUD textHUD;
	private Text title_Text;
	private Text descrption_Text;
	private Text gold_Text;
	private Text silver_Text;
	private Text bronze_Text;
	private TiledSprite achievement1;
	private TiledSprite achievement2;
	private TiledSprite achievement3;
	private TiledSprite achievement4;
	private TiledSprite achievement5;
	private TiledSprite achievement6;
	private TiledSprite achievement7;
	private TiledSprite achievement8;
	private TiledSprite achievement9;
	private TiledSprite achievement10;
	private Rectangle info_button;
	private Sprite ok_button;
	private Sprite info_popup;
	private boolean popup_flag = false;
	private MenuScene menuChildScene;	//menu scene is imported
	private final int MENU_LEFT = 0;
	private final int MENU_RIGHT = 1;
	private int screen_index = 1;
	
	private void createMenuChildScene()
	{
	    menuChildScene = new MenuScene(camera);	//sets up the palette for the screen
	    menuChildScene.setPosition(400, 240);
	    
	    final IMenuItem LeftMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_LEFT, resourcesManager.left_region, vbom), 1.5f, 1);	//creates buttons
	    final IMenuItem RightMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_RIGHT, resourcesManager.right_region, vbom), 1.5f, 1);
	    
	    menuChildScene.addMenuItem(LeftMenuItem);	//adds buttons to the menu
	    menuChildScene.addMenuItem(RightMenuItem);
	    
	    menuChildScene.buildAnimations();		//builds animation for button press; makes button bigger when pressed
	    menuChildScene.setBackgroundEnabled(false);
	    
	    LeftMenuItem.setPosition(info_popup.getX() - 690, info_popup.getY() - 250);	//sets button locations; need to resituate
	    RightMenuItem.setPosition(info_popup.getX() - 110, info_popup.getY() - 250);
	    
	    menuChildScene.setOnMenuItemClickListener(this);	//listen for press, act appropriately
	    
	    setChildScene(menuChildScene);
	    
	}
	
	private void createHUD()
    {
        textHUD = new HUD();
		
		title_Text = new Text(400, 400, resourcesManager.font, "BurgerChickenMushroomSushiGrapesStrawberryCherryTomatoeWatermelonBellpepperMedal", new TextOptions(HorizontalAlign.LEFT), vbom);
        title_Text.setColor(Color.BLACK);
        title_Text.setText("");
        
    	descrption_Text = new Text(400, 304, resourcesManager.small_font, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjklmnopqrstuvwxyz", new TextOptions(HorizontalAlign.LEFT), vbom);
    	descrption_Text.setColor(Color.BLACK);
    	descrption_Text.setAutoWrapWidth(500f);
    	descrption_Text.setAutoWrap(AutoWrap.WORDS);
    	descrption_Text.setText("");
        
    	gold_Text = new Text(100, 196, resourcesManager.font, "GoldMedalat000123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
    	gold_Text.setColor(Color.BLACK);
    	gold_Text.setAnchorCenter(0, 0);
    	gold_Text.setText("");
        
    	silver_Text = new Text(100, 132, resourcesManager.font, "SilverMedalat000123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
    	silver_Text.setColor(Color.BLACK);
    	silver_Text.setAnchorCenter(0, 0);
    	silver_Text.setText("");
        
    	bronze_Text = new Text(100, 68, resourcesManager.font, "BronzeMedalat000123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
    	bronze_Text.setColor(Color.BLACK);
    	bronze_Text.setAnchorCenter(0, 0);
    	bronze_Text.setText("");
        
        textHUD.attachChild(title_Text);
        textHUD.attachChild(descrption_Text);
        textHUD.attachChild(gold_Text);
        textHUD.attachChild(silver_Text);
        textHUD.attachChild(bronze_Text);
        
        textHUD.setVisible(false);
        camera.setHUD(textHUD);
    }
	
	private void updateText()
	{
		switch(screen_index){
    	case 1:
    		title_Text.setText("Burger Medal");
    		descrption_Text.setText("Play a specific amount of games in Classic Mode");
    		gold_Text.setText("Gold Medal at 20");
    		silver_Text.setText("Silver Medal at 10");
    		bronze_Text.setText("Bronze Medal at 5");
    		break;
    	case 2:
    		title_Text.setText("Chicken Medal");
    		descrption_Text.setText("Score a specific amount of points in Classic Mode");
    		gold_Text.setText("Gold Medal at 1000");
    		silver_Text.setText("Silver Medal at 500");
    		bronze_Text.setText("Bronze Medal at 100");
    		break;
    	case 3:
    		title_Text.setText("Mushroom Medal");
    		descrption_Text.setText("Play a specific amount of games in Arcade Mode");
    		gold_Text.setText("Gold Medal at 20");
    		silver_Text.setText("Silver Medal at 10");
    		bronze_Text.setText("Bronze Medal at 5");
    		break;
    	case 4:
    		title_Text.setText("Sushi Medal");
    		descrption_Text.setText("Score a specific amount of points in Arcade Mode");
    		gold_Text.setText("Gold Medal at 1000");
    		silver_Text.setText("Silver Medal at 500");
    		bronze_Text.setText("Bronze Medal at 100");
    		break;
    	case 5:
    		title_Text.setText("Grapes Medal");
    		descrption_Text.setText("Play a specific amount of games");
    		gold_Text.setText("Gold Medal at 200");
    		silver_Text.setText("Silver Medal at 100");
    		bronze_Text.setText("Bronze Medal at 50");
    		break;
    	case 6:
    		title_Text.setText("Strawberry Medal");
    		descrption_Text.setText("Flip a specific amount of foods");
    		gold_Text.setText("Gold Medal at 5000");
    		silver_Text.setText("Silver Medal at 2000");
    		bronze_Text.setText("Bronze Medal at 1000");
    		break;
    	case 7:
    		title_Text.setText("Cherry Medal");
    		descrption_Text.setText("Cook a specific amount of foods");
    		gold_Text.setText("Gold Medal at 5000");
    		silver_Text.setText("Silver Medal at 2000");
    		bronze_Text.setText("Bronze Medal at 1000");
    		break;
    	case 8:
    		title_Text.setText("Tomato Medal");
    		descrption_Text.setText("Obtain a specific combo chain");
    		gold_Text.setText("Gold Medal at 200");
    		silver_Text.setText("Silver Medal at 100");
    		bronze_Text.setText("Bronze Medal at 50");
    		break;
    	case 9:
    		title_Text.setText("Watermelon Medal");
    		descrption_Text.setText("Play a specific amount of Multiplayer Games");
    		gold_Text.setText("Gold Medal at 20");
    		silver_Text.setText("Silver Medal at 10");
    		bronze_Text.setText("Bronze Medal at 5");
    		break;
    	case 10:
    		title_Text.setText("Bellpepper Medal");
    		descrption_Text.setText("Win a specific amount of Multiplayer Games");
    		gold_Text.setText("Gold Medal at 20");
    		silver_Text.setText("Silver Medal at 10");
    		bronze_Text.setText("Bronze Medal at 5");
    		break;
    	}
	}
	
	private void createImages()
    {
		achievement1 = new TiledSprite(400, 230, resourcesManager.achievement_region_0, vbom);
		if(UserData.getInstance().getUnlocksPref(2) == '1')
		{
			achievement1.setCurrentTileIndex(3);
		}
		else if(UserData.getInstance().getUnlocksPref(1) == '1')
		{
			achievement1.setCurrentTileIndex(2);
		}
		else if(UserData.getInstance().getUnlocksPref(0) == '1')
		{
			achievement1.setCurrentTileIndex(1);
		}
		else
		{
			achievement1.setCurrentTileIndex(0);
		}
		achievement1.setVisible(true);
	    attachChild(achievement1);
	    
	    achievement2 = new TiledSprite(400, 230, resourcesManager.achievement_region_1, vbom);
	    if(UserData.getInstance().getUnlocksPref(5) == '1')
		{
			achievement2.setCurrentTileIndex(3);
		}
		else if(UserData.getInstance().getUnlocksPref(4) == '1')
		{
			achievement2.setCurrentTileIndex(2);
		}
		else if(UserData.getInstance().getUnlocksPref(3) == '1')
		{
			achievement2.setCurrentTileIndex(1);
		}
		else
		{
			achievement2.setCurrentTileIndex(0);
		}
		achievement2.setVisible(false);
	    attachChild(achievement2);
	    
	    achievement3 = new TiledSprite(400, 230, resourcesManager.achievement_region_2, vbom);
	    if(UserData.getInstance().getUnlocksPref(8) == '1')
		{
			achievement3.setCurrentTileIndex(3);
		}
		else if(UserData.getInstance().getUnlocksPref(7) == '1')
		{
			achievement3.setCurrentTileIndex(2);
		}
		else if(UserData.getInstance().getUnlocksPref(6) == '1')
		{
			achievement3.setCurrentTileIndex(1);
		}
		else
		{
			achievement3.setCurrentTileIndex(0);
		}
		achievement3.setVisible(false);
	    attachChild(achievement3);
	    
	    achievement4 = new TiledSprite(400, 230, resourcesManager.achievement_region_3, vbom);
	    if(UserData.getInstance().getUnlocksPref(11) == '1')
		{
			achievement4.setCurrentTileIndex(3);
		}
		else if(UserData.getInstance().getUnlocksPref(10) == '1')
		{
			achievement4.setCurrentTileIndex(2);
		}
		else if(UserData.getInstance().getUnlocksPref(9) == '1')
		{
			achievement4.setCurrentTileIndex(1);
		}
		else
		{
			achievement4.setCurrentTileIndex(0);
		}
		achievement4.setVisible(false);
	    attachChild(achievement4);
	    
	    achievement5 = new TiledSprite(400, 230, resourcesManager.achievement_region_4, vbom);
	    if(UserData.getInstance().getUnlocksPref(14) == '1')
		{
			achievement5.setCurrentTileIndex(3);
		}
		else if(UserData.getInstance().getUnlocksPref(13) == '1')
		{
			achievement5.setCurrentTileIndex(2);
		}
		else if(UserData.getInstance().getUnlocksPref(12) == '1')
		{
			achievement5.setCurrentTileIndex(1);
		}
		else
		{
			achievement5.setCurrentTileIndex(0);
		}
		achievement5.setVisible(false);
	    attachChild(achievement5);
	    
	    achievement6 = new TiledSprite(400, 230, resourcesManager.achievement_region_5, vbom);
	    if(UserData.getInstance().getUnlocksPref(17) == '1')
		{
			achievement6.setCurrentTileIndex(3);
		}
		else if(UserData.getInstance().getUnlocksPref(16) == '1')
		{
			achievement6.setCurrentTileIndex(2);
		}
		else if(UserData.getInstance().getUnlocksPref(15) == '1')
		{
			achievement6.setCurrentTileIndex(1);
		}
		else
		{
			achievement6.setCurrentTileIndex(0);
		}
		achievement6.setVisible(false);
	    attachChild(achievement6);
	    
	    achievement7 = new TiledSprite(400, 230, resourcesManager.achievement_region_6, vbom);
	    if(UserData.getInstance().getUnlocksPref(20) == '1')
		{
			achievement7.setCurrentTileIndex(3);
		}
		else if(UserData.getInstance().getUnlocksPref(19) == '1')
		{
			achievement7.setCurrentTileIndex(2);
		}
		else if(UserData.getInstance().getUnlocksPref(18) == '1')
		{
			achievement7.setCurrentTileIndex(1);
		}
		else
		{
			achievement7.setCurrentTileIndex(0);
		}
		achievement7.setVisible(false);
	    attachChild(achievement7);
	    
	    achievement8 = new TiledSprite(400, 230, resourcesManager.achievement_region_7, vbom);
	    if(UserData.getInstance().getUnlocksPref(23) == '1')
		{
			achievement8.setCurrentTileIndex(3);
		}
		else if(UserData.getInstance().getUnlocksPref(22) == '1')
		{
			achievement8.setCurrentTileIndex(2);
		}
		else if(UserData.getInstance().getUnlocksPref(21) == '1')
		{
			achievement8.setCurrentTileIndex(1);
		}
		else
		{
			achievement8.setCurrentTileIndex(0);
		}
		achievement8.setVisible(false);
	    attachChild(achievement8);
	    
	    achievement9 = new TiledSprite(400, 230, resourcesManager.achievement_region_8, vbom);
	    if(UserData.getInstance().getUnlocksPref(26) == '1')
		{
			achievement9.setCurrentTileIndex(3);
		}
		else if(UserData.getInstance().getUnlocksPref(25) == '1')
		{
			achievement9.setCurrentTileIndex(2);
		}
		else if(UserData.getInstance().getUnlocksPref(24) == '1')
		{
			achievement9.setCurrentTileIndex(1);
		}
		else
		{
			achievement9.setCurrentTileIndex(0);
		}
		achievement9.setVisible(false);
	    attachChild(achievement9);
	    
	    achievement10 = new TiledSprite(400, 230, resourcesManager.achievement_region_9, vbom);
	    if(UserData.getInstance().getUnlocksPref(29) == '1')
		{
			achievement10.setCurrentTileIndex(3);
		}
		else if(UserData.getInstance().getUnlocksPref(28) == '1')
		{
			achievement10.setCurrentTileIndex(2);
		}
		else if(UserData.getInstance().getUnlocksPref(27) == '1')
		{
			achievement10.setCurrentTileIndex(1);
		}
		else
		{
			achievement10.setCurrentTileIndex(0);
		}
		achievement10.setVisible(false);
	    attachChild(achievement10);
    }
	
	private void createButtons()
	{
        
		info_button = new Rectangle(400, 240, 320, 320, vbom){
            public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
            {
                if (touchEvent.isActionUp())
                {
                	popup_flag = true;
                	ok_button.setVisible(true);
                	info_popup.setVisible(true);
                	menuChildScene.setVisible(false);
                	switch(screen_index){
                	case 1:
                		achievement1.setVisible(false);
                		break;
                	case 2:
                		achievement2.setVisible(false);
                		break;
                	case 3:
                		achievement3.setVisible(false);
                		break;
                	case 4:
                		achievement4.setVisible(false);
                		break;
                	case 5:
                		achievement5.setVisible(false);
                		break;
                	case 6:
                		achievement6.setVisible(false);
                		break;
                	case 7:
                		achievement7.setVisible(false);
                		break;
                	case 8:
                		achievement8.setVisible(false);
                		break;
                	case 9:
                		achievement9.setVisible(false);
                		break;
                	case 10:
                		achievement10.setVisible(false);
                		break;
                	}	
                	textHUD.setVisible(true);
                	updateText();
                }
                return true;
            };
        };
        info_button.setVisible(false);
        registerTouchArea(info_button);
        attachChild(info_button);
        
        ok_button = new Sprite(680, 420, resourcesManager.ok_region, vbom){
            public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
            {
                if (touchEvent.isActionUp())
                {
                	popup_flag = false;
                	ok_button.setVisible(false);
                	info_popup.setVisible(false);
                	menuChildScene.setVisible(true);
                	switch(screen_index){
                	case 1:
                		achievement1.setVisible(true);
                		break;
                	case 2:
                		achievement2.setVisible(true);
                		break;
                	case 3:
                		achievement3.setVisible(true);
                		break;
                	case 4:
                		achievement4.setVisible(true);
                		break;
                	case 5:
                		achievement5.setVisible(true);
                		break;
                	case 6:
                		achievement6.setVisible(true);
                		break;
                	case 7:
                		achievement7.setVisible(true);
                		break;
                	case 8:
                		achievement8.setVisible(true);
                		break;
                	case 9:
                		achievement9.setVisible(true);
                		break;
                	case 10:
                		achievement10.setVisible(true);
                		break;
                	}	
                	textHUD.setVisible(false);
                }
                return true;
            };
        };
        registerTouchArea(ok_button);
        ok_button.setVisible(false);
        attachChild(ok_button);
	}
	
	@Override
	public void createScene() 
	{
		//UserData.getInstance().init(activity.getBaseContext());
		createBackground();
	    createHUD();
	    createButtons();
	    createImages();
	    createMenuChildScene();
	}
	
	private void createBackground()
	{
	    attachChild(new Sprite(400, 240, resourcesManager.achievements_background_region, vbom)
	    {
	        @Override
	        protected void preDraw(GLState pGLState, Camera pCamera) //makes picture quality smoother
	        {
	            super.preDraw(pGLState, pCamera);
	            pGLState.enableDither();
	            
	        }
	        
	    });
	    
	    info_popup = new Sprite(400, 240, resourcesManager.popup_region, vbom);
	    info_popup.setVisible(false);
	    attachChild(info_popup);
	    
	}

	@Override
	public void onBackKeyPressed() 	//exit app when backed
	{
		SceneManager.getInstance().loadMenuScene(engine, this.getSceneType());
	}
	

	@Override
	public SceneType getSceneType()
	{
	    return SceneType.SCENE_ACHIEVEMENTS;
	}
	
	
	@Override
	public void disposeScene() 
	{
		camera.setHUD(null);
        camera.setCenter(400, 240);
        menuChildScene.detachSelf();
		menuChildScene.dispose();
        this.detachSelf();
        this.dispose();
	}
	
	private void buttonClick(int type)
	{
		if (type == 0)
		{
			if(screen_index > 1)
			{
				screen_index--;
			}
			else
			{
				screen_index = 10;
			}
		}
		else if (type == 1)
		{
			if(screen_index < 10)
			{
				screen_index++;
			}
			else
			{
				screen_index = 1;
			}
		}
		
		updateText();
		updateImages();
	}
	
	private void updateImages()
	{
		switch(screen_index){
		case 1:
			achievement10.setVisible(false);
			achievement1.setVisible(true);
			achievement2.setVisible(false);
			break;
		case 2:
			achievement1.setVisible(false);
			achievement2.setVisible(true);
			achievement3.setVisible(false);
			break;
		case 3:
			achievement2.setVisible(false);
			achievement3.setVisible(true);
			achievement4.setVisible(false);
			break;
		case 4:
			achievement3.setVisible(false);
			achievement4.setVisible(true);
			achievement5.setVisible(false);
			break;
		case 5:
			achievement4.setVisible(false);
			achievement5.setVisible(true);
			achievement6.setVisible(false);
			break;
		case 6:
			achievement5.setVisible(false);
			achievement6.setVisible(true);
			achievement7.setVisible(false);
			break;
		case 7:
			achievement6.setVisible(false);
			achievement7.setVisible(true);
			achievement8.setVisible(false);
			break;
		case 8:
			achievement7.setVisible(false);
			achievement8.setVisible(true);
			achievement9.setVisible(false);
			break;
		case 9:
			achievement8.setVisible(false);
			achievement9.setVisible(true);
			achievement10.setVisible(false);
			break;
		case 10:
			achievement9.setVisible(false);
			achievement10.setVisible(true);
			achievement1.setVisible(false);
			break;
		}
	}
	
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY)
	{
		switch(pMenuItem.getID())
	    {
			case MENU_LEFT:
				if(!popup_flag)
				{
					buttonClick(MENU_LEFT);
				}
				
	            return true;
			case MENU_RIGHT:
				if(!popup_flag)
				{
					buttonClick(MENU_RIGHT);
				}
				
				return true;
	        default:
	            return false;     
	    }
	}

}	